package handler

import (
	"github.com/gin-gonic/gin"
	"tvmanager/model"
)

func Add(c *gin.Context) {
	title := c.PostForm("title")
	content := c.PostForm("content")
	times := c.PostForm("times")
	if title == "" || content == "" || times == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不可为空",
		})
		return
	}
	data := model.Video{
		Title:   title,
		Content: content,
		Times:   times,
	}
	err := model.DB.Create(&data).Debug().Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func List(c *gin.Context) {
	var video []model.Video
	model.DB.Find(&video)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": video,
	})
	return
}
